Learning ggplot2

We are learning ggplot2 and it’s going to be amazing.

## -- Attaching packages --------------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.2     v purrr   0.3.4
## v tibble  3.0.4     v dplyr   1.0.2
## v tidyr   1.1.2     v stringr 1.4.0
## v readr   1.4.0     v forcats 0.5.0
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
## 
## -- Column specification --------------------------------------------------------
## cols(
##   region = col_character(),
##   state = col_character(),
##   code = col_character(),
##   park_name = col_character(),
##   type = col_character(),
##   visitors = col_double(),
##   year = col_double()
## )
## 
## 
## -- Column specification --------------------------------------------------------
## cols(
##   region = col_character(),
##   state = col_character(),
##   code = col_character(),
##   park_name = col_character(),
##   type = col_character(),
##   visitors = col_double(),
##   year = col_double()
## )
## 
## 
## -- Column specification --------------------------------------------------------
## cols(
##   region = col_character(),
##   state = col_character(),
##   code = col_character(),
##   park_name = col_character(),
##   type = col_character(),
##   visitors = col_double(),
##   year = col_double()
## )
## 
## 
## -- Column specification --------------------------------------------------------
## cols(
##   region = col_character(),
##   state = col_character(),
##   code = col_character(),
##   park_name = col_character(),
##   type = col_character(),
##   visitors = col_double(),
##   year = col_double()
## )
## 
## 
## -- Column specification --------------------------------------------------------
## cols(
##   region = col_character(),
##   state = col_character(),
##   code = col_character(),
##   park_name = col_character(),
##   type = col_character(),
##   visitors = col_double(),
##   year = col_double()
## )
ggplot(data = se) +
    geom_point(aes(x = year, y = visitors/1000000, color = state)) +
    labs( x = 'Year', y = 'Visitors in millions', title = 'Park Visitation') +
    theme_light(base_size = 10) +
    theme(legend.title = element_blank(), 
          axis.text.x = element_text(angle = 45, hjust = 1, size = 6)) +
    scale_x_continuous(n.breaks = 50) + scale_y_continuous(n.breaks = 20)

library(RColorBrewer)
library(colorRamps)

## See available palettes
display.brewer.all()

## You need to expand palette size
colourCount <- length(unique(visit_16$park_name)) # number of levels
getPalette <- colorRampPalette(brewer.pal(9, "Spectral"))

g <- ggplot(data = visit_16, aes(x = state, y = visitors, fill = park_name)) +
    geom_col() +
    coord_flip() +
    scale_fill_manual(values = colorRampPalette(brewer.pal(9, "Greens"))(colourCount))
g

ggsave("national_parks.png", g, width = 15, height = 10)
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
ggplotly(g)
acad_vis <- ggplot(data = acadia, aes(x = year, y = visitors)) + 
  geom_point() +
  geom_line() +
  geom_smooth(color = "red") +
  labs(title = "Acadia National Park Visitation",
       y = "Visitation",
       x = "Year") +
  theme_bw()

ggplotly(acad_vis)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'